const request = require('request')
let https = require("https")
var qs = require('querystring');
let fs = require("fs")

exports.requests = function (options) {
    return new Promise((resolve, reject) => {
        request(options, function (error, response) {
            if (error) throw new Error(error);

            resolve(response.body)
        });
    })

}

exports.lt = function (msg, token, id) {
    // console.log(token, 17)
    return new Promise((resolve, reject) => {
        var param = qs.stringify({
            'access_token': token
        });
        var options = {
            hostname: 'aip.baidubce.com',
            path: '/rpc/2.0/unit/bot/chat?' + param,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json; charset=UTF-8'
            }
        };
        var req = https.request(
            options,
            function (res) {
                // 在标准输出中查看运行结果
                let aa = res.pipe(process.stdout);
                resolve(res)
            }
        );
        var postData = {
            'log_id': id,
            'version': '2.0',
            'request': {
                'user_id': '88888',
                'query_info': {
                    'asr_candidates': [],
                    'type': 'TEXT',
                    'source': 'KEYBOARD'
                },
                'bernard_level': 1,
                'updates': '',
                'query': msg,
                'client_session': '{"client_results":"", "candidate_options":[]}'
            },
            'bot_session': '',
            'bot_id': '1447963'
        };

        // 携带数据发送https请求
        req.write(JSON.stringify(postData));

        req.end();
    })

}
